// server.js
const path = require('path');
const fs = require('fs');
const express = require('express')
const server = require('express')()
const proxy = require('http-proxy-middleware');
const history = require('connect-history-api-fallback');
const ecstatic = require('ecstatic');
const renderHtml = function(fileName) {
  let emptyHtml = fs.readFileSync(path.resolve(__dirname + fileName), 'utf8');
  return emptyHtml;
}

server.get('/view/*', (req, res) => {
  res.send(renderHtml('/dist/index.html'));
})

server.use(
  history({
    verbose: true,
    logger: console.log.bind(console),
    rewrites: [
      {
        from: /\/view\/(.*?)/,
        to: function(context) {
          return '/dist/index.html'
        }
      },

      {
        from: /\/public\/(.*?)/,
        to: function(context) {
          return context.parsedUrl.pathname.replace('/public', '')
        }
      },

      {
        from: /\/(.*?)/,
        to: function(context) {
          return '/view/not-found'
        }
      }
    ]
  })
);

server.use(ecstatic({
  root: path.resolve('./dist'),
  baseDir: '/'
}));

console.log('ndoe server http://127.0.0.1:9000')
server.listen(9000)
